package com.example.testjava;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    MyService _service = null;
    boolean _bind = false;
    Messenger _messager = null;

    ServiceConnection _conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i("main", "connected");

//            MyService.MyBinder binder = (MyService.MyBinder)service;
//            _service = binder.getService();

            _messager = new Messenger(service);

            Log.i("main", "get service");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("main", "disconnected");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    void sayHello() {
        Message msg = Message.obtain(null, MyService.MSG_SAY_HELLO, 0, 0);

        try {
            _messager.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            Bundle bundle = msg.getData();
            if (bundle != null) {
                String name = bundle.getString("name");
                int age = bundle.getInt("age");
                Log.d("ipc", "receive name 、age from server, name:" + name + " age:" + age);

                runOnUiThread(()->{
                    EditText et = findViewById(R.id.editTextTest);
                    et.setText(String.format("%s %d", name, age));
                });
            }
        }
    };

    void getMessage() {
        Message msg = Message.obtain(null, MyService.MSG_GET_MSG, 0, 0);

        msg.replyTo = new Messenger(handler);

        try {
            _messager.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void onClick_test(View view) {
        // 直接赋值
//        EditText et = findViewById(R.id.editTextTest);
//        Date dt = new Date();
//        et.setText(dt.toString());

        // 弹出提示
        //Toast.makeText(this,"按钮被点击了",Toast.LENGTH_SHORT).show();

        // 对话框
//        AlertDialog.Builder dlg = new AlertDialog.Builder(this)
//                .setTitle("测试")
//                .setMessage("测试12334");
//        dlg.show();

        // bind 服务
//        if(_service == null) {
//            Intent intent = new Intent(this, MyService.class);
//
//            Log.i("main", "begin bind");
//
//            // 异步的，后面不能阻塞等待
//            if (!bindService(intent, _conn, BIND_AUTO_CREATE)) {
//                Log.e("main", "bind failed");
//                _bind = false;
//                return;
//            }
//
//            Log.i("main", "bind ok");
//
//            _bind = true;
//            return;
//        }
//
//        EditText et = findViewById(R.id.editTextTest);
//        et.setText(_service.getTime().toString());

        // start 服务
        if (_messager == null) {
            Intent intent = new Intent(this, MyService.class);
            startService(intent);

            // bind 服务
            if (!bindService(intent, _conn, Context.BIND_AUTO_CREATE)) {
                return;
            }

            return;
        }

        //sayHello();

        getMessage();
    }
}